#Replication code for "The Symbolic Benefits of Descriptive and Substantive Representation."
#Last edited: April 25, 2016

#Running the basic analyses
m1ccap <- lm(fairness ~ relevel(des_rep, ref="Two")*sub_rep*black, data=ccap)
m1mturk1 <- lm(fairness ~ relevel(des_rep, ref="Two")*sub_rep*black, data=mturk1)
m1mturk2 <- lm(fairness ~ relevel(des_rep, ref="Two")*sub_rep*black, data=mturk2)
m1mturk3 <- lm(fairness ~ relevel(des_rep, ref="Zero")*black, data=mturk3)

#Table 1: Number of observations by treatment, CCAP (2012)
with(ccap, table(des_rep,sub_rep))
with(subset(ccap, ccap$black==1), table(des_rep,sub_rep))

#Figure 1
whites_white <- data.frame(black="0",des_rep=c("Two","Five","Ten"),sub_rep="White")
whites_black <- data.frame(black="0",des_rep=c("Two","Five","Ten"),sub_rep="Black")
blacks_black <- data.frame(black="1", des_rep=c("Two","Five","Ten"), sub_rep="Black")
blacks_white <- data.frame(black="1", des_rep=c("Two","Five","Ten"), sub_rep="White")
ccap_estimates2 <- predict(m1ccap, whites_white, interval="confidence", level=0.90)
ccap_estimates4 <- predict(m1ccap,whites_black, interval="confidence", level=0.90)
ccap_estimates1 <- predict(m1ccap, blacks_white, interval="confidence", level=0.90)
ccap_estimates3 <- predict(m1ccap, blacks_black, interval="confidence", level=0.90)

pdf("figure1a.pdf")
par(mar=c(5,5,5,5))
plot(1, type="n", xlim=c(1,2), ylim=c(0,1), xlab="", ylab="Perceived Fairness", xaxt="n")
mtext(text=c(2,5,10), side=1, line=0, at=c(9/8,10/8,11/8))
mtext(text=c(2,5,10), side=1, line=0, at=c(13/8,14/8,15/8))
mtext(text=c("White Neighborhood","Black Neighborhood"), side=1, line=1.5, at=c(10/8,14/8))
points(c(9/8, 10/8, 11/8), ccap_estimates1[,1], pch=(21), cex=1.5, col="black", bg="black")
points(c(13/8, 14/8, 15/8), ccap_estimates3[,1], pch=c(21), cex=1.5, col="black", bg="black")
arrows(x0=c(9/8, 10/8, 11/8), y0=ccap_estimates1[,1], x1=c(9/8, 10/8, 11/8), y1=ccap_estimates1[,2], angle=90, length=0.1)
arrows(x0=c(9/8, 10/8, 11/8), y0=ccap_estimates1[,1], x1=c(9/8, 10/8, 11/8), y1=ccap_estimates1[,3], angle=90, length=0.1)
arrows(x0=c(13/8, 14/8, 15/8), y0=ccap_estimates3[,1], x1=c(13/8, 14/8, 15/8), y1=ccap_estimates3[,2], angle=90, length=0.1)
arrows(x0=c(13/8, 14/8, 15/8), y0=ccap_estimates3[,1], x1=c(13/8, 14/8, 15/8), y1=ccap_estimates3[,3], angle=90, length=0.1)
abline(v = 1.5, lty=2)
dev.off()

pdf("figure1b.pdf")
par(mar=c(5,5,5,5))
plot(1, type="n", xlim=c(1,2), ylim=c(0,1), xlab="", ylab="Perceived Fairness", xaxt="n")
mtext(text=c(2,5,10), side=1, line=0, at=c(9/8,10/8,11/8))
mtext(text=c(2,5,10), side=1, line=0, at=c(13/8,14/8,15/8))
mtext(text=c("White Neighborhood","Black Neighborhood"), side=1, line=1.5, at=c(10/8,14/8))
points(c(9/8, 10/8, 11/8), ccap_estimates2[,1], pch=c(1), cex=1.5, col="black")
points(c(13/8, 14/8, 15/8), ccap_estimates4[,1], pch=c(1), cex=1.5, col="black")
arrows(c(9/8, 10/8, 11/8), y0=ccap_estimates2[,1], c(9/8, 10/8, 11/8), y1=ccap_estimates2[,2], angle=90, length=0.1)
arrows(c(9/8, 10/8, 11/8), y0=ccap_estimates2[,1], c(9/8, 10/8, 11/8), y1=ccap_estimates2[,3], angle=90, length=0.1)
arrows(c(13/8, 14/8, 15/8), y0=ccap_estimates4[,1], c(13/8, 14/8, 15/8), y1=ccap_estimates4[,2], angle=90, length=0.1)
arrows(c(13/8, 14/8, 15/8), y0=ccap_estimates4[,1], c(13/8, 14/8, 15/8), y1=ccap_estimates4[,3], angle=90, length=0.1)
abline(v = 1.5, lty=2)
dev.off()

#Table 2: Number of obserations by treatment, MTurk (2013)
with(mturk2, table(des_rep,sub_rep))
with(subset(mturk2, mturk2$black==1), table(des_rep,sub_rep))

#Figure 2
whites_new3 <- data.frame(black="0",des_rep=c("Two","Five","Ten"),sub_rep="White")
whites_new4 <- data.frame(black="0",des_rep=c("Two","Five","Ten"),sub_rep="Black")
mturk_estimates1w <- predict(m1mturk2, whites_new3, interval="confidence", level=0.90)
mturk_estimates2w <- predict(m1mturk2, whites_new4, interval="confidence", level=0.90)
blacks_new3 <- data.frame(black="1",des_rep=c("Two","Five","Ten"),sub_rep="White")
blacks_new4 <- data.frame(black="1",des_rep=c("Two","Five","Ten"),sub_rep="Black")
mturk_estimates1b <- predict(m1mturk2, blacks_new3, interval="confidence", level=0.90)
mturk_estimates2b <- predict(m1mturk2, blacks_new4, interval="confidence", level=0.90)
whites_new5 <- data.frame(black="0",des_rep=c("Zero","Three","Four","Eight"),sub_rep="White")
whites_new6 <- data.frame(black="0",des_rep=c("Zero","Three","Four","Eight"),sub_rep="Black")
mturk_estimates3w <- predict(m1mturk3, whites_new5, interval="confidence", level=0.90)
mturk_estimates4w <- predict(m1mturk3, whites_new6, interval="confidence", level=0.90)
blacks_new5 <- data.frame(black="1",des_rep=c("Zero","Three","Four","Eight"),sub_rep="White")
blacks_new6 <- data.frame(black="1",des_rep=c("Zero","Three","Four","Eight"),sub_rep="Black")
mturk_estimates3b <- predict(m1mturk3, blacks_new5, interval="confidence", level=0.90)
mturk_estimates4b <- predict(m1mturk3, blacks_new6, interval="confidence", level=0.90)

pdf("figure2a.pdf")
par(mar=c(5,5,5,5))
plot(1, type="n", xlim=c(1,2), ylim=c(0,1), xlab="", ylab="Perceived Fairness", xaxt="n")
mtext(text=c(2,5,10), side=1, line=0, at=c(9/8,10/8,11/8))
mtext(text=c(2,5,10), side=1, line=0, at=c(13/8,14/8,15/8))
mtext(text=c("Affirmative Action Plan","Testing Plan"), side=1, line=1.5, at=c(10/8,14/8))
points(c(9/8, 10/8, 11/8), mturk_estimates2b[,1], pch=(21), cex=1.5, col="black", bg="black")
points(c(13/8, 14/8, 15/8), mturk_estimates1b[,1], pch=c(21), cex=1.5, col="black", bg="black")
arrows(x0=c(9/8, 10/8, 11/8), y0=mturk_estimates2b[,1], x1=c(9/8, 10/8, 11/8), y1=mturk_estimates2b[,2], angle=90, length=0.1)
arrows(x0=c(9/8, 10/8, 11/8), y0=mturk_estimates2b[,1], x1=c(9/8, 10/8, 11/8), y1=mturk_estimates2b[,3], angle=90, length=0.1)
arrows(x0=c(13/8, 14/8, 15/8), y0=mturk_estimates1b[,1], x1=c(13/8, 14/8, 15/8), y1=mturk_estimates1b[,2], angle=90, length=0.1)
arrows(x0=c(13/8, 14/8, 15/8), y0=mturk_estimates1b[,1], x1=c(13/8, 14/8, 15/8), y1=mturk_estimates1b[,3], angle=90, length=0.1)
abline(v = 1.5, lty=2)
dev.off()

pdf("figure2b.pdf")
par(mar=c(5,5,5,5))
plot(1, type="n", xlim=c(1,2), ylim=c(0,1), xlab="", ylab="Perceived Fairness", xaxt="n")
mtext(text=c(2,5,10), side=1, line=0, at=c(9/8,10/8,11/8))
mtext(text=c(2,5,10), side=1, line=0, at=c(13/8,14/8,15/8))
mtext(text=c("Affirmative Action Plan","Testing Plan"), side=1, line=1.5, at=c(10/8,14/8))
points(c(9/8, 10/8, 11/8), mturk_estimates2w[,1], pch=(21), cex=1.5, col="black", bg="white")
points(c(13/8, 14/8, 15/8), mturk_estimates1w[,1], pch=c(21), cex=1.5, col="black", bg="white")
arrows(x0=c(9/8, 10/8, 11/8), y0=mturk_estimates2w[,1], x1=c(9/8, 10/8, 11/8), y1=mturk_estimates2w[,2], angle=90, length=0.1)
arrows(x0=c(9/8, 10/8, 11/8), y0=mturk_estimates2w[,1], x1=c(9/8, 10/8, 11/8), y1=mturk_estimates2w[,3], angle=90, length=0.1)
arrows(x0=c(13/8, 14/8, 15/8), y0=mturk_estimates1w[,1], x1=c(13/8, 14/8, 15/8), y1=mturk_estimates1w[,2], angle=90, length=0.1)
arrows(x0=c(13/8, 14/8, 15/8), y0=mturk_estimates1w[,1], x1=c(13/8, 14/8, 15/8), y1=mturk_estimates1w[,3], angle=90, length=0.1)
abline(v = 1.5, lty=2)
dev.off()

#Table 3: Number of observations by treatment, MTurk (2014)
with(mturk3, table(des_rep))
with(subset(mturk3, mturk3$black==1), table(des_rep))

#Figure 3
pdf("figure3a.pdf")
par(mar=c(5,5,5,5))
plot(1, type="n", xlim=c(0,1), ylim=c(0,1), xlab="", ylab="Perceived Fairness", xaxt="n")
mtext(text=c(0,2,3,4,5,8,10), side=1, line=0, at=c(5/12,6/12,7/12,8/12,9/12,10/12,11/12))
mtext(text=c(2,5,10), side=1, line=0, at=c(1/12, 2/12,3/12))
mtext(text=c("Affirmative Action Plan","Testing Plan"), side=1, line=1.5, at=c(2/12,8/12))
points(c(1/12,2/12,3/12), mturk_estimates2b[,1], pch=(21), cex=1.5, col="black", bg="black")
points(c(6/12, 9/12, 11/12), mturk_estimates1b[,1], pch=c(21), cex=1.5, col="black", bg="black")
points(c(5/12,7/12,8/12,10/12), mturk_estimates3b[,1], pch=21, cex=1.5, col="black", bg="black")
arrows(x0=c(1/12,2/12,3/12), y0=mturk_estimates2b[,1], x1=c(1/12,2/12,3/12), y1=mturk_estimates2b[,2], angle=90, length=0.1)
arrows(x0=c(1/12,2/12,3/12), y0=mturk_estimates2b[,1], x1=c(1/12,2/12,3/12), y1=mturk_estimates2b[,3], angle=90, length=0.1)
arrows(x0=c(6/12, 9/12, 11/12), y0=mturk_estimates1b[,1], x1=c(6/12, 9/12, 11/12), y1=mturk_estimates1b[,2], angle=90, length=0.1)
arrows(x0=c(6/12, 9/12, 11/12), y0=mturk_estimates1b[,1], x1=c(6/12, 9/12, 11/12), y1=mturk_estimates1b[,3], angle=90, length=0.1)
arrows(x0=c(5/12,7/12,8/12,10/12), y0=mturk_estimates3b[,1], x1=c(5/12,7/12,8/12,10/12), y1=mturk_estimates3b[,2], angle=90, length=0.1)
arrows(x0=c(5/12,7/12,8/12,10/12), y0=mturk_estimates3b[,1], x1=c(5/12,7/12,8/12,10/12), y1=mturk_estimates3b[,3], angle=90, length=0.1)
abline(v = 4/12, lty=2)
dev.off()

pdf("figure3b.pdf")
par(mar=c(5,5,5,5))
plot(1, type="n", xlim=c(0,1), ylim=c(0,1), xlab="", ylab="Perceived Fairness", xaxt="n")
mtext(text=c(0,2,3,4,5,8,10), side=1, line=0, at=c(5/12,6/12,7/12,8/12,9/12,10/12,11/12))
mtext(text=c(2,5,10), side=1, line=0, at=c(1/12, 2/12,3/12))
mtext(text=c("Affirmative Action Plan","Testing Plan"), side=1, line=1.5, at=c(2/12,8/12))
points(c(1/12,2/12,3/12), mturk_estimates2w[,1], pch=(21), cex=1.5, col="black", bg="white")
points(c(6/12, 9/12, 11/12), mturk_estimates1w[,1], pch=c(21), cex=1.5, col="black", bg="white")
points(c(5/12,7/12,8/12,10/12), mturk_estimates3w[,1], pch=21, cex=1.5, col="black", bg="white")
arrows(x0=c(1/12,2/12,3/12), y0=mturk_estimates2w[,1], x1=c(1/12,2/12,3/12), y1=mturk_estimates2w[,2], angle=90, length=0.1)
arrows(x0=c(1/12,2/12,3/12), y0=mturk_estimates2w[,1], x1=c(1/12,2/12,3/12), y1=mturk_estimates2w[,3], angle=90, length=0.1)
arrows(x0=c(6/12, 9/12, 11/12), y0=mturk_estimates1w[,1], x1=c(6/12, 9/12, 11/12), y1=mturk_estimates1w[,2], angle=90, length=0.1)
arrows(x0=c(6/12, 9/12, 11/12), y0=mturk_estimates1w[,1], x1=c(6/12, 9/12, 11/12), y1=mturk_estimates1w[,3], angle=90, length=0.1)
arrows(x0=c(5/12,7/12,8/12,10/12), y0=mturk_estimates3w[,1], x1=c(5/12,7/12,8/12,10/12), y1=mturk_estimates3w[,2], angle=90, length=0.1)
arrows(x0=c(5/12,7/12,8/12,10/12), y0=mturk_estimates3w[,1], x1=c(5/12,7/12,8/12,10/12), y1=mturk_estimates3w[,3], angle=90, length=0.1)
abline(v = 4/12, lty=2)
dev.off()


#Appendix
#Table 1 data
summary(ccap)
summary(mturk1)


#Figure 1
whites_new1 <- data.frame(black="0",des_rep=c("Two","Five","Ten"),sub_rep="White")
whites_new2 <- data.frame(black="0",des_rep=c("Two","Five","Ten"),sub_rep="Black")
ccap_estimates1 <- predict(m1ccap, whites_new1, interval="confidence", level=0.90)
ccap_estimates2 <- predict(m1ccap,whites_new2, interval="confidence", level=0.90)

pdf("ccap_whites.pdf")
par(mar=c(5,5,5,5))
plot(1, type="n", xlim=c(1,3), ylim=c(0,1), xlab="Number of black representatives", ylab="Perceived Fairness\n(White respondents)", xaxt="n")
par(new=TRUE)
axis(1, at=c(1, 2, 3), labels=c("Two", "Five", "Ten"))
par(new=TRUE, adj=0.5)
points(c(1,2,3), ccap_estimates1[,1], pch=21, cex=1.5, col="black", bg="white")
arrows(x0=c(1,2,3), y0=ccap_estimates1[,1], x1=c(1,2,3), y1=ccap_estimates1[,2], angle=90, length=0.1)
arrows(x0=c(1,2,3), y0=ccap_estimates1[,1], x1=c(1,2,3), y1=ccap_estimates1[,3], angle=90, length=0.1)
points(c(1,2,3), ccap_estimates2[,1], pch=21, cex=1.5, col="black", bg="black")
arrows(x0=c(1,2,3), y0=ccap_estimates2[,1], x1=c(1,2,3), y1=ccap_estimates2[,2], angle=90, length=0.1)
arrows(x0=c(1,2,3), y0=ccap_estimates2[,1], x1=c(1,2,3), y1=ccap_estimates2[,3], angle=90, length=0.1)
legend("topleft", c("White neighborhood","Black neighborhood"),pch=c(1,16),col=c("black","black"),bg=c("white","black"))
dev.off()

#Figure 2
blacks_new1 <- data.frame(black="1",des_rep=c("Two","Five","Ten"),sub_rep="White")
blacks_new2 <- data.frame(black="1",des_rep=c("Two","Five","Ten"),sub_rep="Black")
ccap_estimates1b <- predict(m1ccap, blacks_new1, interval="confidence", level=0.90)
ccap_estimates2b <- predict(m1ccap, blacks_new2, interval="confidence", level=0.90)

pdf("ccap_blacks.pdf")
par(mar=c(5,5,5,5))
plot(1, type="n", xlim=c(1,3), ylim=c(0,1), xlab="Number of black representatives", ylab="Perceived Fairness\n(Black respondents)", xaxt="n")
par(new=TRUE)
axis(1, at=c(1, 2, 3), labels=c("Two", "Five", "Ten"))
par(new=TRUE, adj=0.5)
points(c(1,2,3), ccap_estimates1b[,1], pch=22, cex=1.5, col="black", bg="white")
arrows(x0=c(1,2,3), y0=ccap_estimates1b[,1], x1=c(1,2,3), y1=ccap_estimates1b[,2], angle=90, length=0.1)
arrows(x0=c(1,2,3), y0=ccap_estimates1b[,1], x1=c(1,2,3), y1=ccap_estimates1b[,3], angle=90, length=0.1)
points(c(1,2,3), ccap_estimates2b[,1], pch=22, cex=1.5, col="black", bg="black")
arrows(x0=c(1,2,3), y0=ccap_estimates2b[,1], x1=c(1,2,3), y1=ccap_estimates2b[,2], angle=90, length=0.1)
arrows(x0=c(1,2,3), y0=ccap_estimates2b[,1], x1=c(1,2,3), y1=ccap_estimates2b[,3], angle=90, length=0.1)
legend("topleft", c("White neighborhood","Black neighborhood"),pch=c(0,15),col=c("black","black"),bg=c("white","black"))
dev.off()

#Figure 3
whites_new1 <- data.frame(black="0",des_rep=c("Two","Five","Ten"),sub_rep="White")
whites_new2 <- data.frame(black="0",des_rep=c("Two","Five","Ten"),sub_rep="Black")
mturk_estimates1 <- predict(m1mturk1, whites_new1, interval="confidence", level=0.90)
mturk_estimates2 <- predict(m1mturk1,whites_new2, interval="confidence", level=0.90)
blacks_new1 <- data.frame(black="1",des_rep=c("Two","Five","Ten"),sub_rep="White")
blacks_new2 <- data.frame(black="1",des_rep=c("Two","Five","Ten"),sub_rep="Black")
mturk_estimates1b <- predict(m1mturk1, blacks_new1, interval="confidence", level=0.90)
mturk_estimates2b <- predict(m1mturk1, blacks_new2, interval="confidence", level=0.90)

pdf("mturk0_whites.pdf")
par(mar=c(5,5,5,5))
plot(1, type="n", xlim=c(1,3), ylim=c(0,1), xlab="Number of black representatives", ylab="Perceived Fairness\n(White respondents)", xaxt="n")
par(new=TRUE)
axis(1, at=c(1, 2, 3), labels=c("Two", "Five", "Ten"))
par(new=TRUE, adj=0.5)
points(c(1,2,3), mturk_estimates1[,1], pch=21, cex=1.5, col="black", bg="white")
arrows(x0=c(1,2,3), y0=mturk_estimates1[,1], x1=c(1,2,3), y1=mturk_estimates1[,2], angle=90, length=0.1)
arrows(x0=c(1,2,3), y0=mturk_estimates1[,1], x1=c(1,2,3), y1=mturk_estimates1[,3], angle=90, length=0.1)
points(c(1,2,3), mturk_estimates2[,1], pch=21, cex=1.5, col="black", bg="black")
arrows(x0=c(1,2,3), y0=mturk_estimates2[,1], x1=c(1,2,3), y1=mturk_estimates2[,2], angle=90, length=0.1)
arrows(x0=c(1,2,3), y0=mturk_estimates2[,1], x1=c(1,2,3), y1=mturk_estimates2[,3], angle=90, length=0.1)
legend("topleft", c("White neighborhood","Black neighborhood"),pch=c(1,16),col=c("black","black"),bg=c("white","black"))
dev.off()

#Figure 4
pdf("mturk0_blacks.pdf")
par(mar=c(5,5,5,5))
plot(1, type="n", xlim=c(1,3), ylim=c(0,1), xlab="Number of black representatives", ylab="Perceived Fairness\n(Black respondents)", xaxt="n")
par(new=TRUE)
axis(1, at=c(1, 2, 3), labels=c("Two", "Five", "Ten"))
par(new=TRUE, adj=0.5)
points(c(1,2,3), mturk_estimates1b[,1], pch=22, cex=1.5, col="black", bg="white")
arrows(x0=c(1,2,3), y0=mturk_estimates1b[,1], x1=c(1,2,3), y1=mturk_estimates1b[,2], angle=90, length=0.1)
arrows(x0=c(1,2,3), y0=mturk_estimates1b[,1], x1=c(1,2,3), y1=mturk_estimates1b[,3], angle=90, length=0.1)
points(c(1,2,3), mturk_estimates2b[,1], pch=22, cex=1.5, col="black", bg="black")
arrows(x0=c(1,2,3), y0=mturk_estimates2b[,1], x1=c(1,2,3), y1=mturk_estimates2b[,2], angle=90, length=0.1)
arrows(x0=c(1,2,3), y0=mturk_estimates2b[,1], x1=c(1,2,3), y1=mturk_estimates2b[,3], angle=90, length=0.1)
legend("topleft", c("White neighborhood","Black neighborhood"),pch=c(0,15),col=c("black","black"),bg=c("white","black"))
dev.off()

with(mturk1, table(des_rep,sub_rep))
with(subset(mturk1, mturk1$black==1), table(des_rep,sub_rep))


#Table 2
m1ccap <- lm(fairness ~ relevel(des_rep, ref="Two")*sub_rep*black, data=ccap)
m1mturk1 <- lm(fairness ~ relevel(des_rep, ref="Two")*sub_rep*black, data=mturk1)
m1mturk2 <- lm(fairness ~ relevel(des_rep, ref="Two")*sub_rep*black, data=mturk2)
m1mturk3 <- lm(fairness ~ relevel(des_rep, ref="Zero")*black, data=mturk3)
library(apsrtable)
apsrtable(m1ccap, m1mturk1, m1mturk2, m1mturk3, model.names=c("Study One", "Replication", "Study Two","Study Three"),
          stars="default")

#Table 3
ccap$des_rep <- as.factor(ccap$des_rep)
ccap$sub_rep <- as.factor(ccap$sub_rep)
m2ccap <- lm(fairness ~ relevel(des_rep, ref="Two")*sub_rep*black + ed + gender + inc + ideology, data=ccap)
m3ccap <- lm(fairness ~ relevel(des_rep, ref="Two")*sub_rep*ideology + ed + gender + inc, data=subset(ccap, ccap$black==0))
m4ccap <- lm(fairness ~ relevel(des_rep, ref="Two")*sub_rep + ed + gender + inc + ideology, data=subset(ccap, ccap$black==1)) 

apsrtable(m2ccap, m3ccap, model.names=c("Model 1", "Model 2"), stars="default")

#Additional robustness checks

#Computing alpha reliability statistics for DV index
library(psych)
attach(ccap)
ccap.dv <- with(ccap, data.frame(dv_comm, dv_process, dv_comp))
detach(ccap)
attach(mturk1)
mturk1.dv <- with(mturk1, data.frame(dv_comm, dv_process, dv_comp))
detach(mturk1)
attach(mturk2)
mturk2.dv <- with(mturk2, data.frame(dv_comm, dv_process, dv_comp))
detach(mturk2)
attach(mturk3)
mturk3.dv <- with(mturk3, data.frame(dv_comm, dv_process, dv_comp))
detach(mturk3)

alpha(ccap.dv)
alpha(mturk1.dv)
alpha(mturk2.dv)
alpha(mturk3.dv)

#Testing question order effects

table(mturk1$dv_com,mturk1$comm_order)
table(mturk2$dv_comm,mturk2$comm_order)
table(mturk3$dv_comm,mturk3$comm_order)
chisq.test(x=mturk3$dv_comm,y=mturk3$comm_order)

table(mturk1$dv_process,mturk1$process_order)
chisq.test(x=mturk1$dv_com,y=mturk1$comm_order)
chisq.test(x=mturk1$dv_process,y=mturk1$process_order)
chisq.test(x=mturk1$dv_comp,y=mturk1$comp_order)


chisq.test(x=mturk2$dv_comm,y=mturk2$comm_order)
chisq.test(x=mturk2$dv_process,y=mturk2$process_order)
chisq.test(x=mturk2$dv_comp,y=mturk2$comp_order)

chisq.test(x=mturk3$dv_comm,y=mturk3$comm_order)
chisq.test(x=mturk3$dv_process,y=mturk3$process_order)
chisq.test(x=mturk3$dv_comp,y=mturk3$comp_order)

mturk1$proc_first <- 0
mturk1$proc_first[mturk1$process_order==1] <- 1
mturk2$proc_first <- 0
mturk2$proc_first[mturk2$process_order==1] <- 1
mturk3$proc_first <- 0
mturk3$proc_first[mturk3$process_order==1] <- 1

#Testing whether the results differ for those who got the process question first
m2mturk1 <- lm(dv_process ~ relevel(des_rep, ref="Two")*sub_rep*black*proc_first, data=mturk1)
m2mturk2 <- lm(dv_process ~ relevel(des_rep, ref="Two")*sub_rep*black*proc_first, data=mturk2)
m2mturk3 <- lm(dv_process ~ relevel(des_rep, ref="Zero")*black*proc_first, data=mturk3)


#Checking that our models work the same separately, using logit instead of OLS

m2ccapOLS <- lm(dv_comm ~ relevel(des_rep, ref="Two")*sub_rep*black, data=ccap)
m2ccapLogit <- polr(as.factor(dv_comm) ~ relevel(des_rep, ref="Two")*sub_rep*black, data=ccap)
m3ccap <- lm(dv_process ~ relevel(des_rep, ref="Two")*sub_rep*black, data=ccap)
m3ccapLogit <- polr(as.factor(dv_process) ~ relevel(des_rep, ref="Two")*sub_rep*black, data=ccap)
m4ccap <- lm(dv_comp ~ relevel(des_rep, ref="Two")*sub_rep*black, data=ccap)
m4ccapLogit <- polr(as.factor(dv_comp) ~ relevel(des_rep, ref="Two")*sub_rep*black, data=ccap)


m2mturk2 <- lm(dv_comm ~ relevel(des_rep, ref="Two")*sub_rep*black, data=mturk2)
m2mturk2Logit <- polr(as.factor(dv_comm) ~ relevel(des_rep, ref="Two")*sub_rep*black, data=mturk2)
m3mturk2 <- lm(dv_process ~ relevel(des_rep, ref="Two")*sub_rep*black, data=mturk2)
m3mturk2Logit <- polr(as.factor(dv_process) ~ relevel(des_rep, ref="Two")*sub_rep*black, data=mturk2)
m4mturk2 <- lm(dv_comp ~ relevel(des_rep, ref="Two")*sub_rep*black, data=mturk2)
m4mturk2Logit <- polr(as.factor(dv_comp) ~ relevel(des_rep, ref="Two")*sub_rep*black, data=mturk2)

m2mturk3 <- lm(dv_comm ~ relevel(des_rep, ref="Zero")*black, data=mturk3)
m3mturk3 <- lm(dv_process ~ relevel(des_rep, ref="Zero")*black, data=mturk3)
m4mturk3 <- lm(dv_comp ~ relevel(des_rep, ref="Zero")*black, data=mturk3)

#Checking to see  if results are affected by percent white/black
ccap$white_perc <- ccap$H6
ccap$white_perc[ccap$white_perc==997] <- NA

ccap$black_perc <- ccap$H7
ccap$black_perc[ccap$black_perc==997] <- NA

m5ccap <- lm(fairness ~ relevel(des_rep, ref="Two")*sub_rep*black + black_perc + white_perc, data=ccap)

